package com.founder.chd_daily_query.service.impl;

import com.founder.chd_daily_query.entity.query.ConfigPowerStationAreaEntity;
import com.founder.chd_daily_query.entity.query.LandCoalPurchasingplanEntity;
import com.founder.chd_daily_query.repo.powerPlant.PowerPlantRepo;
import com.founder.chd_daily_query.repo.query.LandCoalPurchasingplanRepo;
import com.founder.chd_daily_query.service.PowerPlantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PowerPlantServiceImpl implements PowerPlantService {

    @Autowired
    private PowerPlantRepo powerPlantRepo;
    @Autowired
    private LandCoalPurchasingplanRepo landCoalPurchasingplanRepo;

    @Override
    public List<ConfigPowerStationAreaEntity> powerPlant(String area) {
        if(area==null){
            return powerPlantRepo.findAll();
        }
        ConfigPowerStationAreaEntity configPowerStationAreaEntity =new ConfigPowerStationAreaEntity();
        configPowerStationAreaEntity.setAreaName(area);

        Example<ConfigPowerStationAreaEntity> of = Example.of(configPowerStationAreaEntity);

        return powerPlantRepo.findAll(of);
    }

    @Override
    public List<String> area() {
        return powerPlantRepo.getArea();
    }

    @Override
    public List priorityQuery(String power) {
        List<LandCoalPurchasingplanEntity> result = new ArrayList<>();

        List<Object> list  = landCoalPurchasingplanRepo.priorityQuery(power);
        for(int i=0;i<list.size();i++){
            Object[] s = (Object[]) list.get(i);
            LandCoalPurchasingplanEntity entity=new LandCoalPurchasingplanEntity();
            entity.setContractId(s[0].toString());
            entity.setContractAmount(Float.parseFloat(s[2].toString()));
            entity.setQnet(Integer.parseInt(s[3].toString()));
            entity.setStandardCoalUnitprice(s[1].toString());
            entity.setStandardCoalUnitprice(s[4].toString());
            try {
                entity.setEffectiveDateB(new SimpleDateFormat("yyyy-MM-dd").parse(s[5].toString()));
                entity.setEffectiveDateE(new SimpleDateFormat("yyyy-MM-dd").parse(s[6].toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            entity.setContractName(s[7].toString());
            result.add(entity);
        }
        return result;
    }
    @Override
    public Map queryPercentage(String power) {
        Map result = new HashMap();
        //长协市场角度
        List percentageList  = landCoalPurchasingplanRepo.queryPercentage(power);
        Double sumContractAmount = 0.0;
        List<LandCoalPurchasingplanEntity> landCoalEntity = new ArrayList<>();
        result.put("年度合同",0);
        result.put("自产煤",0);
        result.put("市场采购",0);
        for(int i=0;i<percentageList.size();i++){
            Object[] obj = (Object[]) percentageList.get(i);
            sumContractAmount += Double.parseDouble(obj[1].toString());
            result.put(obj[0].toString(),Double.parseDouble(obj[1].toString()));
        }
        //运输方式角度
        List transportList = landCoalPurchasingplanRepo.queryTransport(power);

        Double ship = 0.0;
        Double land = 0.0;
        for(int i=0;i<transportList.size();i++){
            Object[] obj = (Object[]) transportList.get(i);
            if(obj[0].toString().equals("船运")){
                ship+=Double.parseDouble(obj[1].toString());
            }else{
                land+=Double.parseDouble(obj[1].toString());
            }
        }
        result.put("水运",ship);
        result.put("陆运",land);
        result.put("SUM",sumContractAmount);
        return result;
    }
}
